package com.ruoyi.project.web.quesInfo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.ApiConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.web.quesInfo.domain.QuesInfo;
import com.ruoyi.project.web.quesInfo.service.IQuesInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 题目信息Controller
 * 
 * @author ruoyi
 * @date 2021-01-22
 */
@Controller
@RequestMapping("/web/quesInfo")
public class QuesInfoController extends BaseController
{
    private String prefix = "web/quesInfo";

    @Autowired
    private IQuesInfoService quesInfoService;

    @RequiresPermissions("web:quesInfo:view")
    @GetMapping()
    public String quesInfo()
    {
        return prefix + "/quesInfo";
    }

    /**
     * 查询题目信息列表
     */
    @RequiresPermissions("web:quesInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QuesInfo quesInfo)
    {
        startPage();
        List<QuesInfo> list = quesInfoService.selectQuesInfoList(quesInfo);
        return getDataTable(list);
    }

    /**
     * 导出题目信息列表
     */
    @RequiresPermissions("web:quesInfo:export")
    @Log(title = "题目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QuesInfo quesInfo)
    {
        List<QuesInfo> list = quesInfoService.selectQuesInfoList(quesInfo);
        ExcelUtil<QuesInfo> util = new ExcelUtil<QuesInfo>(QuesInfo.class);
        return util.exportExcel(list, "quesInfo");
    }

    @Log(title = "题目信息", businessType = BusinessType.IMPORT)
    @RequiresPermissions("web:quesInfo:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<QuesInfo> util = new ExcelUtil<QuesInfo>(QuesInfo.class);
        List<QuesInfo> quesList = util.importExcel(file.getInputStream());
        String message = quesInfoService.importQues(quesList, updateSupport);
        return AjaxResult.success(message);
    }

    @RequiresPermissions("web:quesInfo:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<QuesInfo> util = new ExcelUtil<QuesInfo>(QuesInfo.class);
        List<QuesInfo> list = new ArrayList();
        QuesInfo quesInfo = new QuesInfo();
        quesInfo.setQuesDesc("雾天对安全行车的主要影响是什么?");
        quesInfo.setAnsAnalysis("《道路交通安全法》第二章第十四条规定：应当报废的机动车必须及时办理注销登记。达到报废标准的机动车不得上道路行驶。报废的大型客、货车及其他营运车辆应当在公安机关交通管理部门的监督下解体。");
        quesInfo.setAnsStandard("C");
        quesInfo.setQuesPoints(10);
        quesInfo.setQuesA("行驶阻力大");
        quesInfo.setQuesB("易发生侧滑");
        quesInfo.setQuesC("能见度低");
        quesInfo.setQuesD("视野变窄");
        quesInfo.setAnswerType("2");
        quesInfo.setLearnType("1");
        list.add(quesInfo);
        return util.exportExcel(list,"题目数据");
    }

    /**
     * 新增题目信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存题目信息
     */
    @RequiresPermissions("web:quesInfo:add")
    @Log(title = "题目信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QuesInfo quesInfo)
    {
       if (StringUtils.isNotEmpty(quesInfo.getAnsStandard())){
           quesInfo.setAnsStandard(quesInfo.getAnsStandard().replaceAll(",","&"));
       }
        return toAjax(quesInfoService.insertQuesInfo(quesInfo));
    }

    /**
     * 修改题目信息
     */
    @GetMapping("/edit/{quesId}")
    public String edit(@PathVariable("quesId") Long quesId, ModelMap mmap)
    {
        QuesInfo quesInfo = quesInfoService.selectQuesInfoById(quesId);
        mmap.put("quesInfo", quesInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存题目信息
     */
    @RequiresPermissions("web:quesInfo:edit")
    @Log(title = "题目信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QuesInfo quesInfo)
    {
        if (StringUtils.isNotEmpty(quesInfo.getAnsStandard())){
            quesInfo.setAnsStandard(quesInfo.getAnsStandard().replaceAll(",","&"));
        }
        return toAjax(quesInfoService.updateQuesInfo(quesInfo));
    }

    /**
     * 删除题目信息
     */
    @RequiresPermissions("web:quesInfo:remove")
    @Log(title = "题目信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(quesInfoService.deleteQuesInfoByIds(ids));
    }

    @PostMapping("/toRandomList")
    @ResponseBody
    public Map<String,Object> toRandomList(String learnType, Integer panduan,Integer danxuan,Integer duoxuan)
    {
        StringBuffer ids = new StringBuffer();
        Integer points =0;
        Map<String, Object> panduanMap = quesInfoService.toRandomList(learnType, panduan, ApiConstants.PAN_DUAN);
        if (StringUtils.isNotNull(panduanMap)){
            ids.append(panduanMap.get("quesIds"));
            points+=Integer.parseInt(panduanMap.get("allPoints").toString());
        }
        Map<String, Object> danxuanMap = quesInfoService.toRandomList(learnType, danxuan, ApiConstants.DAN_XUAN);
        if (StringUtils.isNotNull(danxuanMap)){
            if (StringUtils.isNotEmpty(ids.toString())){
                ids.append(","+danxuanMap.get("quesIds"));
            }else {
                ids.append(danxuanMap.get("quesIds"));
            }
            points+=Integer.parseInt(danxuanMap.get("allPoints").toString());
        }
        Map<String, Object> duoxuanMap = quesInfoService.toRandomList(learnType, duoxuan, ApiConstants.DUO_XUAN);
        if (StringUtils.isNotNull(duoxuanMap)){
            if (StringUtils.isNotEmpty(ids.toString())){
                ids.append(","+duoxuanMap.get("quesIds"));
            }else {
                ids.append(duoxuanMap.get("quesIds"));
            }
            points+=Integer.parseInt(duoxuanMap.get("allPoints").toString());
        }
        System.out.println(ids);
        HashMap<String, Object> map = new HashMap<>();
        map.put("quesIds",ids);
        map.put("allPoints",points);
        return map;
    }



}
